app.controller("changeController",['$scope','$http','myPage', function($scope, $http, myPage) {
    // 超级管理员权限
    $http({
        method: 'GET',
        url: '/OA/staff/roles',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    }).success(function (response) {
        $scope.roles=[];
        $scope.roles=response.data;
        console.log($scope.roles)
        function permissions(){
            var rs=false;
            for (var i = 0; i < $scope.roles.length; i++) {
                if ($scope.roles[i] == 7) {
                    rs = true;
                }else{
                    rs= false;
                }
            }
            return rs
        }
        $scope.admin=permissions();
        console.log($scope.admin)
    })
    $scope.myPage={
        currentPage:1,//访问第几页数据，从1开始
        totalItems:0,//数据库中总共有多少条数据
        itemsPerPage: 6,//默认每页展示多少条数据，可更改
        pagesLength: 15,
        perPageOptions: [10, 20, 30, 40, 50,60]//可选择的每页展示多少条数据
    };
    //监测当页码。总数据，每页展示数据个数变化时，重新加载数据
    $scope.$watch(function ()
    {return $scope.myPage.itemsPerPage+' '+$scope.myPage.currentPage+' '+$scope.myPage.totalItems;},getList) ;
    function getList(){
        //获取列表需要时，将页码重置为1
        $scope.myPage.currentPage=myPage.pageNub;
        //传给服务器时，页码从0开始
        // 定义查询按钮
        var listJson={};
        function changeJson(){
            var studentCard=$("#studentcard").val();
            var studentName=$("#studentname").val();
            var staffId=$("#responsible").find("option:selected").val();
            var campus_id=$("#schoolcampus").find("option:selected").val();
            return {pageNumber:myPage.pageNub,staff_id:staffId,name:studentName,sn:studentCard,campus_id:campus_id}
        }
        listJson=changeJson();
        $scope.find=function () {
            listJson=changeJson();
            Json();
        }
        Json();
        function  Json() {
            $http({
                method: 'POST',
                url: '/OA/clazz/list',
                data: $.param(listJson),
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            }).success(function (response) {
                console.log(response)
                $scope.datas=response.data.list;
                $scope.pageSize=response.data.pageSize;
                $scope.pageNumber=response.data.pageNumber;
                $scope.myPage.totalItems=response.data.totalRow;//当获取总数据后，修改默认值;
                $scope.arrs=$scope.datas;
                // 定义一个单击删除按钮时触发的事件，用于删除选中行
                $scope.delete = function($index){
                    if ($index>=0) {
                        if(confirm("是否删除"+$scope.arrs[$index].name+"的信息") ){
                            //将需要删除的卡号传给后台  2017/7/10
                            $http({
                                method: 'POST',
                                url: '/OA/student/delete',
                                data: $.param({id:$scope.arrs[$index].id}),
                                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                            }).success(function (result) {
                                if (result.ok==true) {
                                    $scope.arrs.splice($index,1);
                                }
                            }).error(function(result){
                                alert("删除失败")
                            })
                        }
                    };
                }
                // 退课学费
                $scope.drop={}; //定义空对象；
                $scope.drops=function ($index) {
                    $("#withdraw").modal("show");
                    $scope.drop_text=$scope.arrs[$index];
                    $scope.drop.pact_id=$scope.arrs[$index].id;
                    var record_sn=$("#record_sn").val();
                    $http({
                        method:"POST",
                        url:"/OA/subjectpactclazz/list",
                        data:$.param({pact_id:$scope.drop.pact_id,record_sn:record_sn}),
                        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                    }).success(function (response) {
                        console.log(response)
                        $scope.drops_data=response.data;
                        $scope.drop_save=function () {
                        var tuition_total=$("#tuition_total").val();
                        var before_sale_id=$("#sale_name").attr('data');
                        var before_sale_derate=$("#sale_tuition").val();
                        var tuition_should=$("#tuition_should").val();
                        var tuition_reality=$("#tuition_reality").val();
                        var after_sale_id=$("#saleclass").find("option:selected").val();
                        var after_sale_derate=$scope.after_sale_derate;
                        if(after_sale_derate==""){
                            alert("请填写调档后减免！")
                            return
                        }
                        var tuition=$scope.tuition;
                        if(tuition==""){
                            alert("请填写应退费信息！")
                            return
                        }
                        var dropJson={pact_id:$scope.arrs[$index].id,tuition_total:tuition_total,before_sale_id:before_sale_id,before_sale_derate:before_sale_derate,tuition_should:tuition_should,tuition_reality:tuition_reality,tuition_use:0,after_sale_id:after_sale_id,after_sale_derate:after_sale_derate,tuition:tuition}
                           console.log(dropJson);
                            $.ajax({
                                type:"POST",
                                url:"/OA/dropclazz/create",
                                data:dropJson,
                                dataType:"json",
                                success:function (data) {
                                 if(data.ok==true){
                                     console.log(data)
                                     var clazz_drop_id=data.data;
                                     for(var i=0; i<response.data.length; i++){
                                         // console.log($("#drop_"+response.data[i].id).val());
                                         if(Number($("#drop_"+response.data[i].id).val())>0 && Number($("#drop_"+response.data[i].id).val())<=$scope.drops_data[i].number_lack){
                                             $scope.json={clazz_pact_subject_id:$scope.drops_data[i].id,number:$("#drop_"+response.data[i].id).val(),clazz_drop_id:clazz_drop_id};
                                             $http({
                                                 method:"POST",
                                                 url:"/OA/subjectdropclazz/create",
                                                 data:$.param($scope.json),
                                                 headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                                             }).success(function (response) {
                                                 if(response.ok==true){
                                                     $("#withdraw").modal("hide");
                                                 }
                                             })
                                         }
                                     }
                                 }
                                }
                            })

                        }
                    })
                }
                // 补交学费
                // 定义一个空对象 , 用于更新和保存数据时临时存储
                $scope.prod = {};
                // 补交
                $scope.supplementary=function ($index) {
                    $("#pay").modal("show");
                    $scope.prod.tuition_lack=$scope.arrs[$index].tuition_lack;
                    $scope.prod.tuition_reality=$scope.arrs[$index].tuition_reality;
                    $scope.prod.tuition_total=$scope.arrs[$index].tuition_total;
                    $scope.prod.pact_id=$scope.arrs[$index].id;
                    $scope.increase=function () {
                        var method_id=$("#methodstyle").find("option:selected").val();
                        var method_name=$("#methodstyle").find("option:selected").text();
                        var amount=Number($scope.nowsupplementary);
                        var pact_id=$scope.prod.pact_id;
                        var record_sn=$scope.record_sn;
                        $scope.JSON={method_id:method_id,method_name:method_name,amount:amount,pact_id:pact_id,record_sn:record_sn};
                        $http({
                            method:"POST",
                            url:"/OA/mend/create?type=2",
                            data:$.param($scope.JSON),
                            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                        }).success(function (response) {
                            if(response.ok==true){
                                $("#pay").modal("hide");
                                setTimeout(function () {
                                    window.location.reload()
                                },600)
                            }
                        })
                    }
                }

            })
        }
    }
    //添加学员API
    // time: 2017/9/27
    $scope.detect=function () {
          var studentcard=$scope.student_sn;
           $scope.JSON={property:"sn",value:studentcard};
           console.log($scope.JSON)
           $http({
            method:"POST",
            url:"/OA/student/exist",
            data:$.param($scope.JSON),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function (response) {
               if(response.ok==true){
                 $("#prompting").html("应用通过，卡号存在");
               }else if(response.ok==false){
                   $("#prompting").html("应用未通过，卡号不存在");
               }
          })
    };
    // 咨询教务切换
    $("#presonposition").change(function () {
         var role_id=$("#presonposition").val();
         $.ajax({
             type:"POST",
             url:"/OA/staff/find",
             data:{role_id:role_id},
             dataType:"json",
             success:function (response) {
                 $("#presonname").html("");
                 for (var i = 0; i < response.data.length; i++) {
                     $("#presonname").append('<option value='+response.data[i].id+'>'+response.data[i].username+'</option>')
                 };
             }
         })
    })
    $scope.addstudent=function(){
        var role_id=$("#presonposition").find("option:selected").val();
        var role_name=$("#presonposition").find("option:selected").text();
        var name=$scope.contractname;
        var student_sn=$scope.student_sn;
        var class_id=$("#createclass").find("option:selected").val();
        var class_name=$("#createclass").find("option:selected").text();
        function staff_type() {
            if($("#presonposition").find("option:selected").val()=="1"){
                return 1
            }else if($("#presonposition").find("option:selected").val()=="3"){
                return 2
            }
        }
        var staff_type=staff_type();
        function staff_id() {
            if($("#presonname").find("option:selected").val()==""){
                alert("请选择一位咨询或者教务");
                return false
            }else{
                return $("#presonname").find("option:selected").val();
            }
        }
        var staff_id=staff_id();
        if(staff_id==false)
            return;
        $scope.addstudent={name:name,student_sn:student_sn,class_id:class_id,class_name:class_name,staff_type:staff_type,staff_id:staff_id,role_id:role_id,role_name:role_name}
        console.log($scope.addstudent);
        $http({
            method: 'POST',
            url: '/OA/clazz/create',
            data: $.param($scope.addstudent),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function (response){
            if (response.ok== true) {
                $("#createContract").modal('hide')
                setTimeout(function () {
                    window.location.reload()
                },600)
            };
        })
    }
    $scope.change = function(selectedLimit){
        $scope.page.limit = selectedLimit.limit;
    }
    $scope.pageChanged = function(){
        $scope.page.limit = $scope.selectedLimit.limit;
    }
    $scope.setPage = function (go) {
        $scope.length = Math.ceil($scope.total/$scope.selectedLimit.limit);
        console.log($scope.length);
        if(go > $scope.length){
            $scope.page.pageNo =  $scope.length;
            console.log($scope.length);
        }else{
            $scope.page.pageNo=go;
        }
    };
}]);


